﻿
using Dapper;
 
using System; 
using System.Collections.Generic;
using System.Data; 
using System.Linq; 

namespace hCommand {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public abstract class AbsTaskCSComp : AbsTaskComp
    {

       
        
        public long createIn2OutTask(AbsCoreStock cs, long des) {
            //将该浅货位的货物移动到该浅货位同组下的空货位  csdList[0].rLoctionId==null ? 0: csdList[0].rLoctionId
            var whLoc =getById<CoreWhLoc>(cs.locId);
            if (whLoc == null) {
                throw new Exception(cs.boxCode + "不在库存");
            }
            TaskResDto res = createTask( cs, (int)des);
            long stockId = cs.id;
            int src = cs.locId; 
            long goodDiff = cs.deepDiff;
            bool b = I18nConfig.isLoc(src);
            bool e = I18nConfig.isLoc(des);
            if (b && e) {
                UpdateStock(stockId, StockStatus.移库中);
                LocCoreUtil.UpdataWhLoc(this, src, goodDiff);
                LocCoreUtil.UpdataWhLoc(this, des, goodDiff); 
            }  else if (b && !e) { 
                UpdateStock(stockId, StockStatus.入库中);
                LocCoreUtil.UpdataWhLoc(this, des, goodDiff);
               
            } else if (!b && e) {
                UpdateStock(stockId, StockStatus.出库中);
                LocCoreUtil.UpdataWhLoc(this, src, goodDiff);
            } else { 
                UpdateStock(stockId, StockStatus.库外移动); 
            }
            return res.paramId;
        } 
        public TaskResDto createOutTask( AbsCoreStock cs, int des, string relay = "") {
            long goodDiff = cs.deepDiff;
            TaskResDto res = createTask( cs, des, relay);
            UpdateStock( cs.id, change2Status(cs,des));
            LocCoreUtil.UpdataWhLoc(this, cs.locId, goodDiff);
            return res;
        }
        public TaskResDto createInTask(AbsCoreStock cs, int des,  String relay = "") {
            if (0 == des || null == cs) {
                throw new Exception(cs.id + " 任务下发失败！");
            }
            TaskResDto res = createTask(  cs, des, relay);
            UpdateStock(  cs.id, change2Status(cs,des));
            LocCoreUtil.UpdataWhLoc(this, des, cs.deepDiff);
            return res;
        }


        virtual protected StockStatus change2Status(AbsCoreStock cs, int des) {
            bool srcb = I18nConfig.isLoc(cs.locId);
            bool desb = I18nConfig.isLoc(des);
            if (srcb) {
                if (desb) {
                   return StockStatus.移库中;
                } else {
                    return StockStatus.出库中;
                }
            } else {
                if (desb) {
                    return StockStatus.入库中;
                } else {
                    return StockStatus.库外移动;
                }
            }
        }

        public bool UpdateStock( long stockId, StockStatus status) {
            string sql = "update  Core_Stock set status=@status , change_Time=sgj_get_sys_now where id=@Id";
            int count = exec(sql, new { id = stockId, status = (int)status });
            return true;
        }


       
    }
}

